﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Dune2k1
{
    public class Map
    {
        TileSet tileset;
        string filepath;
        UInt16 width, height;
        UInt16[,] tiles;
        Vector2[,] tilePositions;
        LightPoint topLeft;

        const int VIEWPORT_WIDTH = 800;
        const int VIEWPORT_HEIGHT = 600;

        const int VIEWPORT_TILEWIDTH = 26;
        const int VIEWPORT_TILEHEIGHT = 20;

        public Map(string filepath, TileSet tileset)
        {
            this.filepath = filepath;
            this.tileset = tileset;

            topLeft = new LightPoint(0, 0);

            LoadMap();
        }

        private void LoadMap()
        {
            BinaryReader r = new BinaryReader(File.Open(filepath, FileMode.Open));
            width = r.ReadUInt16();
            height = r.ReadUInt16();

            tiles = new UInt16[width, height];
            tilePositions = new Vector2[width, height];

            int size = width * height;

            int x, y;
            for (int i = 0; i < size; i++)
            {
                x = i % width;
                y = i / height;

                tiles[x, y] = r.ReadUInt16();
                r.ReadUInt16();
                tilePositions[x, y] = new Vector2(x * 32, y * 32);
            }
        }

        public void Move(Vector2 amount)
        {
            /*
             *                      _ _
             *               _     //` `\
             *           _,-"\%   // /``\`\
             *      ~^~ >__^  |% // /  } `\`\
             *             )  )%// / }  } }`\`\
             *            /  (%/'/.\_/\_/\_/\`/
             *           (    '         `-._`
             *            \   ,     (  \   _`-.__.-;%>
             *           /_`\ \      `\ \." `-..-'`
             *          ``` /_/`"-=-'`/_/
             *             ```       ```
             *              
             *             Here be dragons!
             */
            if (tilePositions[0, 0].X + amount.X > 0 || tilePositions[0, 0].Y + amount.Y > 0)
            {
                return;
            }

            if ((int)Math.Abs(tilePositions[0, 0].X + amount.X) / 32 > width - VIEWPORT_TILEWIDTH || (int)Math.Abs(tilePositions[0, 0].Y + amount.Y) / 32 > height - VIEWPORT_TILEHEIGHT)
            {
                return;
            }

            topLeft.X = (int)Math.Abs(tilePositions[0, 0].X + amount.X) / 32;
            topLeft.Y = (int)Math.Abs(tilePositions[0, 0].Y + amount.Y) / 32;

            tilePositions[0, 0].X = tilePositions[0, 0].X + amount.X;
            tilePositions[0, 0].Y = tilePositions[0, 0].Y + amount.Y;

            for (int x = topLeft.X; x < topLeft.X + VIEWPORT_TILEWIDTH; x++)
            {
                for (int y = topLeft.Y; y < topLeft.Y + VIEWPORT_TILEHEIGHT; y++)
                {
                    tilePositions[x, y].X = (x * 32) + tilePositions[0, 0].X;
                    tilePositions[x, y].Y = (y * 32) + tilePositions[0, 0].Y;
                }
            }
        }

        public void Draw(GameTime t)
        {
            for (int x = topLeft.X; x < topLeft.X + VIEWPORT_TILEWIDTH; x++)
            {
                for (int y = topLeft.Y; y < topLeft.Y + VIEWPORT_TILEHEIGHT; y++)
                {
                    Engine.Sprite.Draw(tileset.Tiles[tiles[x, y]].Texture, tilePositions[x, y], Color.White);
                }
            }
        }
    }
}
